package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.P;
import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;

public class LoginFragment extends GenericoFragment {

    private TextView textViewNomeTelaLogin;
    private EditText editTextEmailLogin;
    private EditText editTextSenhaLogin;
    private Button buttonEntrarLogin;
    private Button buttonCadastreseLogin;
    private TextView textViewEsqueciSenhaLogin;
    private ProgressBar progressBarLogin;

    private AcoesLoginFragment activity;
    private LoginTask task;

    View.OnClickListener clickButtonEntrarLogin = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = editTextEmailLogin.getText().toString();
            String senha = editTextSenhaLogin.getText().toString();

            if (email.isEmpty()) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_email);
            } else if (senha.isEmpty()) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_senha);
            } else {
                if (email.equals(activity.getPreferencesEmail()) && senha.equals(activity.getPreferencesSenha())) {
                    if (activity.isConnected()) {
                        task = new LoginTask();
                        task.execute();
                    } else {
                        activity.goDashboard(activity.getPreferencesAluno());
                    }
                } else {
                    if (activity.isConnected()) {
                        task = new LoginTask();
                        task.execute();
                    } else {
                        activity.onSpeakAndMakeText(R.string.excecao_sem_conexao_internet);
                    }
                }
            }
        }
    };

    View.OnClickListener clickButtonCadastreseLogin = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.onClickCadastrese();
        }
    };

    View.OnClickListener clickTextViewEsqueciSenhaLogin = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.onClickEsqueciASenha();
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = (AcoesLoginFragment) getActivity();

        String email = activity.getPreferencesEmail();
        String senha = activity.getPreferencesSenha();

        editTextEmailLogin.setText(email);
        editTextSenhaLogin.setText(senha);

        if (email != null && !email.trim().equals(P.STRING_EMPITY) && senha != null && !senha.trim().equals(P.STRING_EMPITY)) {
            task = new LoginTask();
            task.execute();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);

        textViewNomeTelaLogin = (TextView) view.findViewById(R.id.textViewNomeTelaLogin);
        editTextEmailLogin = (EditText) view.findViewById(R.id.editTextEmailLogin);
        editTextSenhaLogin = (EditText) view.findViewById(R.id.editTextSenhaLogin);
        buttonEntrarLogin = (Button) view.findViewById(R.id.buttonEntrarLogin);
        buttonCadastreseLogin = (Button) view.findViewById(R.id.buttonCadastreseLogin);
        textViewEsqueciSenhaLogin = (TextView) view.findViewById(R.id.textViewEsqueciSenhaLogin);
        progressBarLogin = (ProgressBar) view.findViewById(R.id.progressBarLogin);

        buttonEntrarLogin.setOnClickListener(clickButtonEntrarLogin);
        buttonCadastreseLogin.setOnClickListener(clickButtonCadastreseLogin);
        textViewEsqueciSenhaLogin.setOnClickListener(clickTextViewEsqueciSenhaLogin);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            hideComponents();
            progressBarLogin.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void showComponents() {
        textViewNomeTelaLogin.setVisibility(View.VISIBLE);
        editTextEmailLogin.setVisibility(View.VISIBLE);
        editTextSenhaLogin.setVisibility(View.VISIBLE);
        buttonEntrarLogin.setVisibility(View.VISIBLE);
        buttonCadastreseLogin.setVisibility(View.VISIBLE);
        textViewEsqueciSenhaLogin.setVisibility(View.VISIBLE);
        progressBarLogin.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideComponents() {
        textViewNomeTelaLogin.setVisibility(View.GONE);
        editTextEmailLogin.setVisibility(View.GONE);
        editTextSenhaLogin.setVisibility(View.GONE);
        buttonEntrarLogin.setVisibility(View.GONE);
        buttonCadastreseLogin.setVisibility(View.GONE);
        textViewEsqueciSenhaLogin.setVisibility(View.GONE);
        progressBarLogin.setVisibility(View.GONE);
    }

    private class LoginTask extends AsyncTask<Void, Void, Aluno> {

        @Override
        protected void onPreExecute() {
            hideComponents();
            progressBarLogin.setVisibility(View.VISIBLE);
            activity.onSpeak(R.string.aguarde);
        }

        @Override
        protected Aluno doInBackground(Void... params) {
            return activity.onClickEntrar(editTextEmailLogin.getText().toString(), editTextSenhaLogin.getText().toString());
        }

        @Override
        protected void onPostExecute(Aluno aAluno) {
            showComponents();
            progressBarLogin.setVisibility(View.GONE);

            if (aAluno != null) {
                activity.goDashboard(aAluno);
                activity.putPreferencesAluno(aAluno);
                activity.onFinish();
            } else {
                activity.onSpeakAndMakeText(R.string.excecao_autenticacao_mausucedida);
            }
        }
    }

    public interface AcoesLoginFragment extends AcoesGenericoFragment {
        Aluno onClickEntrar(String email, String senha);
        void onClickCadastrese();
        void onClickEsqueciASenha();
        void goDashboard(Aluno aluno);
        String getPreferencesEmail();
        String getPreferencesSenha();
    }
}
